/**
 * 
 */
package com.sx.cms.action;

import nl.captcha.Captcha;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.alibaba.fastjson.JSONObject;
import com.opensymphony.xwork2.ModelDriven;
import com.sx.cms.biz.IUserInfoBiz;
import com.sx.cms.entity.UserInfoEntity;
import com.sx.core.BaseAction;
import com.sx.feesunit.exception.SXException;
import com.sx.util.Secret;

/**
 * @author Administrator
 *
 */
@SuppressWarnings("serial")
public class UserInfoAction extends BaseAction implements ModelDriven<UserInfoEntity> {
	private UserInfoEntity userinfo = new UserInfoEntity();
	private IUserInfoBiz userinfobiz;
	private Log log = LogFactory.getLog(this.getClass());
	/**
	 * 铁锁用户登录
	 * 登录成功0
	 * 验证码不对-1
	 * 用户名或密码错误
	 **/
	public void login() throws SXException{
		try {
			String code = getRequest().getParameter("imagecode");
			Captcha captcha = (Captcha)getSession().getAttribute("simpleCaptcha");
			if (captcha.isCorrect(code)) {
				UserInfoEntity user =  userinfobiz.getUserInfo(userinfo);
				if(user!=null&&Secret.getMD5(userinfo.getPassword().trim()).equals(user.getPassword())){
					//登录成功
					getSession().setAttribute("userinfo", user);
					outJsonString("{\"success\":true,\"errorcode\":0}");
				}else{
					outJsonString("{\"success\":true,\"errorcode\":-2}");
				}
			}else{
				outJsonString("{\"success\":false,\"errorcode\":-1}");
			}
		} catch (Exception e) {
			// TODO: handle exception
			outJsonString("{\"success\":false,\"errorcode\":-2}");
			log.error("铁锁用户登录",e);
			throw new SXException();
		}
	}
	/**
	 *跳转到登录页面 
	 **/
	public String goToTsLogin(){
		return "loginpage";
	}
	/**
	 *检查用户邮箱是否存在 
	 * @throws SXException 
	 **/
	public void checkEmailIsAvliable() throws SXException{
		String str = "({\"haveflag\":false})";
		try {
			String email = userinfo.getEmail();
			UserInfoEntity userinfo = new UserInfoEntity();
			userinfo.setEmail(email);
			boolean flag = userinfobiz.checkLoginNameIsAvliable(userinfo);
			if (flag) {
				str = "({\"haveflag\":true})";
			}
			outJsonString(str);
		} catch (Exception e) {
			outJsonString(str);
			log.error("检查用户邮箱是否存在",e);
			// TODO: handle exception
		}
	}
	/**
	 *检查登录名是否存在 
	 * @throws SXException 
	 **/
	public void checkLoginNameIsAvliable() throws SXException{
		String str = "({\"haveflag\":false})";
		try {
			String loginname = userinfo.getLoginname();
			UserInfoEntity userinfo = new UserInfoEntity();
			userinfo.setLoginname(loginname);
			boolean flag = userinfobiz.checkLoginNameIsAvliable(userinfo);
			if (flag) {
				str = "({\"haveflag\":true})";
			}
			outJsonString(str);
		} catch (Exception e) {
			outJsonString(str);
			log.error("检查登录名是否存在",e);
			// TODO: handle exception
		}
	}
	/**
	 *注册信息 
	 * 返回 -2  数据库插入失败 
	 * 返回 -1  验证码不正确
	 * 返回 0   成功
	 * @throws SXException 
	 **/
	public void registerUser(){
		try {
			String code = getRequest().getParameter("imagecode");
			Captcha captcha = (Captcha)getSession().getAttribute("simpleCaptcha");
			Integer id = new Integer(0);
			if (captcha.isCorrect(code)) {
				userinfo.setPassword(Secret.getMD5(userinfo.getPassword()));
				id =  userinfobiz.addUserInfo(userinfo);
				if(id!=null||!id.equals(0)){
					outJsonString("{\"success\":true,\"errorcode\":0}");
				}else{
					outJsonString("{\"success\":false,\"errorcode\":-2}");
				}
			}else{
				outJsonString("{\"success\":false,\"errorcode\":-1}");
			}
		} catch (Exception e) {
			outJsonString("{\"success\":true}");
			log.error("注册用户",e);
			// TODO: handle exception
		}
		
	}
	/* (non-Javadoc)
	 * @see com.opensymphony.xwork2.ModelDriven#getModel()
	 */
	public UserInfoEntity getModel() {
		// TODO Auto-generated method stub
		return userinfo;
	}
	/**
	 * @return the userinfobiz
	 */
	public IUserInfoBiz getUserinfobiz() {
		return userinfobiz;
	}
	/**
	 * @param userinfobiz the userinfobiz to set
	 */
	public void setUserinfobiz(IUserInfoBiz userinfobiz) {
		this.userinfobiz = userinfobiz;
	}

}
